/* =============================================================================
 * Step 1: construct human capital
 * ===========================================================================*/

/*******************************************************************************

								US 
		
*******************************************************************************/	

/*******************************************************************************
sample selection
*******************************************************************************/
use "data\US.dta", clear

drop if outlier

keep if male == 1

gen wexp = min(age - eduyrs - 6, age - 18) 

rename ybirth byear
drop if byear < 1935
drop if byear > 1984

rename lnearnings lwage
rename asecwt perwt 

/*******************************************************************************
discretize
*******************************************************************************/
egen wexp_group = cut(wexp), at(0(5)40)
drop if wexp_group == .
gen byear_group = 5*floor(byear/5)

collapse (count) obs=lwage [pw = perwt], by(year wexp_group byear_group)

bys year: egen obs_total = total(obs)
gen share = obs / obs_total

/*******************************************************************************
merge the estimated experience and cohort profiles
*******************************************************************************/
gen plot_wexp = wexp_group
merge m:1 plot_wexp using "temp\us_exp_profile.dta"
keep if _m == 3
drop _m

gen plot_coh = byear_group
merge m:1 plot_coh using "temp\us_coh_profile.dta"
keep if _m == 3
drop _m

sort year wexp_group byear_group
gen h_wgt = profile_wexp_us * profile_coh_us * share
gen h_exp = profile_wexp_us * share
gen h_coh = profile_coh_us * share

preserve
collapse (sum) h_wgt, by(year)
rename h h_us
save "temp\construct_h_us.dta", replace
restore

collapse (sum) h_wgt h_exp h_coh, by(year)
rename h* h*_us
save "temp\decomposition_h_us.dta", replace



/*******************************************************************************

										China 
		
*******************************************************************************/		


use "data\China.dta", clear

drop if outlier
keep if male == 1

gen wexp = min(age - eduyrs - 6, age - 18) 

rename ybirth byear
drop if byear < 1935
drop if byear > 1984

rename lnearnings lwage


/*******************************************************************************
discretize
*******************************************************************************/
egen wexp_group = cut(wexp), at(0(5)40)
drop if wexp_group == .
gen byear_group = 5*floor(byear/5)

collapse (count) obs=lwage, by(year wexp_group byear_group)

bys year: egen obs_total = total(obs)
gen share = obs / obs_total

/*******************************************************************************
merge the estimated experience and cohort profiles
*******************************************************************************/
gen plot_wexp = wexp_group
merge m:1 plot_wexp using "temp\cn_exp_profile.dta"
keep if _m == 3
drop _m

gen plot_coh = byear_group
merge m:1 plot_coh using "temp\cn_coh_profile.dta"
keep if _m == 3
drop _m

sort year wexp_group byear_group
gen h_wgt = profile_wexp * profile_coh * share
gen h_exp = profile_wexp * share
gen h_coh = profile_coh * share

preserve
collapse (sum) h = h_wgt, by(year)
rename h h_cn
save "temp\construct_h_cn.dta", replace
restore

collapse (sum) h_wgt h_exp h_coh, by(year)
rename h* h*_cn
save "temp\decomposition_h_cn.dta", replace





/* =============================================================================
 * Step 2: plug human capital into growth accounting
 * ===========================================================================*/

use "data\macro.dta", clear
keep if inrange(year,1986,2012)

/*******************************************************************************
	merge with estimated hc series
*******************************************************************************/
merge 1:1 year using "temp\construct_h_cn.dta"
drop _merge
merge 1:1 year using "temp\construct_h_us.dta"
drop _merge

/*******************************************************************************
	prepare relevant variables
*******************************************************************************/
foreach c in cn us	{	
	*** get share parameter
	gen alpha_`c' = 1 - s_`c'

	*** define per-worker variables
	gen y_`c' = Y_`c' / L_`c'
	gen k_`c' = K_`c' / L_`c'
	
	*** write in logs
	gen lny_`c' = log(y_`c')
	gen lnk_`c' = log(k_`c')
	gen lnh_`c' = log(h_`c')
}


/*******************************************************************************
	growth accounting (first differences)
*******************************************************************************/
tsset year

* we do this, instead of directly accumulated, because alpha is allowed to change, although the effect is small.
foreach c in cn us	{
	
	gen dlny_`c' = D.lny_`c'
	replace dlny_`c' = 0 in 1
	
	gen dlnk_`c' = D.lnk_`c'
	replace dlnk_`c' = 0 in 1
	gen clnk_`c' = alpha_`c' * dlnk_`c'
	
	
	gen dlnh_`c' = D.lnh_`c'
	replace dlnh_`c' = 0 in 1
	gen clnh_`c' = (1-alpha_`c') * dlnh_`c'
	
	gen dlnA_`c' = dlny_`c' - clnk_`c' - clnh_`c'
}

/*******************************************************************************
	accumulative sum
*******************************************************************************/
foreach c in cn us	{
	
	gen acc_lny_`c' = sum(dlny_`c')
	gen acc_lnk_`c' = sum(clnk_`c')
	gen acc_lnh_`c' = sum(clnh_`c')
	gen acc_lnA_`c' = sum(dlnA_`c')
	
}

dis %9.1f acc_lnk_us[_N]/acc_lny_us[_N]*100
dis %9.1f acc_lnk_cn[_N]/acc_lny_cn[_N]*100
dis %9.1f acc_lnh_us[_N]/acc_lny_us[_N]*100
dis %9.1f acc_lnh_cn[_N]/acc_lny_cn[_N]*100
dis %9.1f acc_lnA_us[_N]/acc_lny_us[_N]*100
dis %9.1f acc_lnA_cn[_N]/acc_lny_cn[_N]*100

twoway 	(scatter acc_lny_us year, msymbol(d) msize(medium) connect(l) mcolor(black) lcolor(black))	///
		(scatter acc_lnk_us year, msymbol(th) msize(medium) connect(l) lpattern(dash) mcolor(red) lcolor(red))	///
		(scatter acc_lnh_us year, msymbol(oh) msize(medium) connect(l) lpattern(dash) mcolor(blue) lcolor(blue))	///
		(scatter acc_lnA_us year, msymbol(x)  msize(medium) connect(l) lpattern(dash)  mcolor(green) lcolor(green)),	///
		xlabel(1985(5)2012, labsize(medlarge))  ylabel(0(0.1)0.5, labsize(medlarge)) xtitle("Year", size(large)) 	///
		legend(order(1 "GDP Per Worker" 2 "Physical Capital Per Worker" 3 "Human Capital Per Worker" 4 "TFP (Residual)")	///
		rows(2) pos(6))		///
		title("U.S.", size(large) color(black)) name(us_growth,replace)

		
twoway 	(scatter acc_lny_cn year, msymbol(d) msize(medium) connect(l) mcolor(black) lcolor(black))	///
		(scatter acc_lnk_cn year, msymbol(th) msize(medium) connect(l) lpattern(dash) mcolor(red) lcolor(red))	///
		(scatter acc_lnh_cn year, msymbol(oh) msize(medium) connect(l) lpattern(dash) mcolor(blue) lcolor(blue))	///
		(scatter acc_lnA_cn year, msymbol(x)  msize(medium) connect(l) lpattern(dash) mcolor(green) lcolor(green)),	///
		xlabel(1985(5)2012, labsize(medlarge))  ylabel(0(0.4)2, labsize(medlarge)) xtitle("Year", size(large)) 	///
		legend(order(1 "GDP Per Worker" 2 "Physical Capital Per Worker" 3 "Human Capital Per Worker" 4 "TFP (Residual)")	///
		rows(2) pos(6))		///
		title("China", size(large) color(black)) name(cn_growth,replace)
		
grc1leg us_growth cn_growth, name(growth_accounting, replace)

graph export "figures\growth_accounting.pdf", as(pdf) replace
